package edu.java.homework.hw09.exercises.person;

import java.util.Arrays;

public class PersonSorter {

    /**
     * create array of Person objects
     * 
     * @return Person[] the array from <code>Person</code>objects.
     */
    public static Person[] createPersonsArray() {
	Person[] personArray = new Person[12];
	personArray[0] = new Person("Petyr", "Vasilev", "Petrov", 40,
		Gender.MALE, "Sofiq", 2);
	personArray[1] = new Person("Dimitrichka", "Pencheva", "Nikolova", 32,
		Gender.FEMALE, "Karnobat", 1);
	personArray[2] = new Person("Pencho", "Minchev", "Asenov", 65,
		Gender.MALE, "Blagoevgrad", 3);
	personArray[3] = new Person("Violeta", "Georgieva", "Deneva", 15,
		Gender.FEMALE, "Asenovgrad", 0);
	personArray[4] = new Person("Stoencho", "Borisov", "Dimitrov", 23,
		Gender.MALE, "Montana", 0);
	personArray[5] = new Person("Krasimira", "Zheliazkova", "Popova", 35,
		Gender.FEMALE, "Velingrad", 2);
	personArray[6] = new Person("Sijka", "Kirova", "Vasileva", 43,
		Gender.FEMALE, "Dobrich", 8);
	personArray[7] = new Person("Isuscho", "Hristoskov", "Hristoskov", 15,
		Gender.MALE, "Stara Zagora (Lozenes)", 3);
	personArray[8] = new Person("Krasimira", "Tileva", "Stoianova", 27,
		Gender.FEMALE, "Lovech", 1);
	personArray[9] = new Person("Vasilka", "Petrova", "Aleksandrova", 55,
		Gender.FEMALE, "Shumen", 2);
	personArray[10] = new Person("Zdravka", "Zheliazkova", "Chilikova", 38,
		Gender.FEMALE, "Yambol", 1);
	personArray[11] = new Person("Petyr", "Angelov", "Jordanov", 47,
		Gender.MALE, "Smolian", 2);
	// personArray[12] = null
	return personArray;

    }

    /**
     * Sort the array of Person objects
     * 
     * @param personsArr
     */
    public static void sortsPersonsArray1(Person[] personsArr) {
	if (personsArr == null) {
	    throw new IllegalArgumentException(
		    "personsArr array is null-pointed");
	}
	// 1st way
	Arrays.sort(personsArr);
    }

    public static void sortsPersonsArray2(Person[] personsArr) {
	if (personsArr == null) {
	    throw new IllegalArgumentException(
		    "personsArr array is null-pointed");
	}

	// 2nd way --> selection sort
	int length = personsArr.length;
	for (int i = 0; i <= length - 2; i++) {
	    Person person = personsArr[i];
	    int k = -1;
	    for (int j = i + 1; j <= length - 1; j++) {
		if (person.compareTo(personsArr[j]) > 0) {
		    person = personsArr[j]; // personsArr[j] -> value <- person
		    k = j;
		}
	    }
	    if (k < 0) {
		continue;
	    }
	    Person tempPerson = personsArr[i];
	    personsArr[i] = personsArr[k];
	    personsArr[k] = tempPerson;
	}
    }

    public static void printPersonsArray(Person[] personsArr) {
	for (Person person : personsArr) {
	    System.out.println(person.toString());
	}
    }
}
